//
//  YJMyAchievementViewController.m
//  Sports-Go
//
//  Created by rimi on 16/9/21.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "YJMyAchievementViewController.h"
#import "YJMyAchieveCell.h"
#import "UIImageView+WebCache.h"
//获取屏幕设度，高度
#define kScreenHight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define headViewH    kScreenWidth/3*2
#define headH        kScreenWidth/4
@interface YJMyAchievementViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) NSArray *picSource;
@property (nonatomic,strong) NSArray *contentSource;
@property (nonatomic,strong) NSArray *finishSource;
@property (nonatomic,assign) NSInteger number;
@property (nonatomic,strong) UIImageView *backimage;
@property (nonatomic,strong) UIImageView *headImage;
@property (nonatomic,assign)CGFloat scale;
@property (nonatomic,strong) UILabel *levelLabel;
@end

@implementation YJMyAchievementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
     self.title = @"我的成就";
    //去掉导航栏返回字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.barTintColor = getColor(58, 206, 248, 1);
    _number = 0;
    _dataSource = @[@"匀称标志",@"玲珑有致",@"丰韵娉婷",@"喷火尤物",@"魔鬼身材"];
    _picSource = @[@"美女1",@"美女2",@"美女3",@"美女4",@"美女5"];
    _contentSource = @[@"锻炼达到7小时",@"锻炼达到21小时",@"锻炼达到50小时",@"锻炼达到100小时",@"锻炼达到170小时"];
   
    _finishSource = @[@"未完成",@"未完成",@"未完成",@"未完成",@"未完成"];
    [self.view addSubview:self.tableView];
    
    
   self.tableView.contentInset = UIEdgeInsetsMake(headViewH, 0, 0, 0);
    
   
    
    _backimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -headViewH, kScreenWidth, headViewH)];

   _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headH, headH)];
    self.scale = _backimage.bounds.size.width/_backimage.bounds.size.height;
    _headImage.center = _backimage.center;
    _headImage.image = [UIImage imageNamed:@"女官1"];
    _headImage.contentMode = UIViewContentModeScaleAspectFit;
    self.levelLabel.frame = CGRectMake(0, CGRectGetMaxY(_headImage.frame)+20, screen_size.width, 30);
    self.levelLabel.text = @"我的成就:运动小白";
    [self.tableView addSubview:_backimage];
    [self.tableView addSubview:_headImage];
    [self.tableView addSubview:self.levelLabel];
    
}
#pragma mark -- tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  _dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *headView = [[UIView alloc] initWithFrame:RzCGRectMake(0, 0, 375, 30)];
    headView.backgroundColor = getColor(236, 236, 236, 1);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:RzCGRectMake(0, 0, 375, 29)];
    titleLabel.text = [NSString stringWithFormat:@"       已获得%ld个成就,共5个",_number];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.backgroundColor = [UIColor whiteColor];
    [headView addSubview:titleLabel];
    return headView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    YJMyAchieveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myachieveCell"];
    
    cell.titleLabel.text = _dataSource[indexPath.row];
    cell.headImage.image = [UIImage imageNamed:_picSource[indexPath.row]];
    cell.finishText = _finishSource[indexPath.row];
    cell.contentLabel.text = _contentSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -- lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStyleGrouped];
        _tableView.rowHeight = 80;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[YJMyAchieveCell class] forCellReuseIdentifier:@"myachieveCell"];
        
    }
    return _tableView;
}
- (UILabel *)levelLabel{
    if (!_levelLabel) {
        _levelLabel = [[UILabel alloc]init];
        _levelLabel.textColor = getColor(58, 206, 248, 1);
        _levelLabel.font = [UIFont boldSystemFontOfSize:17];
        _levelLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _levelLabel;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (scrollView.contentOffset.y < -headViewH ) {
        // 高度宽度同时拉伸 从中心放大
        _backimage.frame = CGRectMake(0, offsetY, kScreenWidth, -offsetY);
        CGFloat conY = scrollView.contentOffset.y  + headViewH;
        CGFloat imgH = -(offsetY);
        CGFloat imgW = imgH * self.scale;
        _backimage.frame = CGRectMake(-(imgW - kScreenWidth)/2,offsetY, imgW,imgH);
        
        _headImage.frame =CGRectMake(0, 0, headH-conY, headH-conY);
        _headImage.center = _backimage.center;
       
        
    }
    //NSLog(@"%0.0f",scrollView.contentOffset.y);
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    self.tabBarController.tabBar.hidden = YES;
    [self getSoprtsTime];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)getSoprtsTime{
    NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    if (userName) {
    AVQuery *timeQuery = [AVQuery queryWithClassName:@"t1"];
        
    [timeQuery whereKey:@"userID" equalTo:userName];
      [timeQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
          if (error) {
              [self warnWitherrorInfo:@"网络错误"];
          }else{
              if (objects) {
                  
        [self judgeSportsTime:[objects.lastObject[@"time"]floatValue]];
                  
              }else{
    _headImage.image = [UIImage imageNamed:@"女官1.png"];
              }
          }
          
      }];
        
        
    }


}
- (void)judgeSportsTime:(CGFloat)time{
    if (time >7.0 && time < 21.0) {
        _headImage.image = [UIImage imageNamed:@"美女1.png"];
        _number = 1;
   _finishSource = @[@"已完成",@"未完成",@"未完成",@"未完成",@"未完成"];
        self.levelLabel.text = @"我的成就: 匀称标志";
    }else if (time >= 21.0 && time < 50.0){
        _number = 2;
    _headImage.image = [UIImage imageNamed:@"美女2.png"];
    _finishSource = @[@"已完成",@"已完成",@"未完成",@"未完成",@"未完成"];
        self.levelLabel.text = @"我的成就: 玲珑有致";
    }else if (time >= 50.0 && time < 100.0){
        _number = 3;
     _finishSource = @[@"已完成",@"已完成",@"已完成",@"未完成",@"未完成"];
    _headImage.image = [UIImage imageNamed:@"美女3.png"];
        self.levelLabel.text = @"我的成就: 丰韵娉婷";
    }else if(time >= 100.0 && time <170.0){
        _number = 4;
     _finishSource = @[@"已完成",@"已完成",@"已完成",@"已完成",@"未完成"];
    _headImage.image = [UIImage imageNamed:@"美女4.png"];
        self.levelLabel.text = @"我的成就: 喷火尤物";
    }else if(time >= 170.0){
        _number = 5;
     _finishSource = @[@"已完成",@"已完成",@"已完成",@"已完成",@"已完成"];
    _headImage.image = [UIImage imageNamed:@"美女5.png"];
    self.levelLabel.text = @"我的成就: 魔鬼身材";
    }
    [self.tableView reloadData];
}

- (void)warnWitherrorInfo:(NSString *)error{
    //警示框
    UILabel *label = [[UILabel alloc]initWithFrame:RzCGRectMake(90, 250, 185, 50)];
    label.backgroundColor = getColor(193, 162, 151, 1);
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.text = error;
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [UIView animateWithDuration:2.0 animations:^{
        label.alpha = 0.0;
    } completion:^(BOOL finished) {
        
        [label removeFromSuperview];
    }];
    
    
}

@end
